package cn.stylefeng.guns.modular.basic.service;

import cn.stylefeng.guns.modular.basic.mapper.CommonMapper;
import cn.stylefeng.guns.modular.basic.model.params.CommonParam;
import cn.stylefeng.guns.modular.basic.service.utils.TreeKit;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.tree.LayuiTree;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommonService extends ServiceImpl<CommonMapper,LayuiTree> {

    @Autowired private DeptService deptService;

    private List<LayuiTree> tree(CommonParam paramCondition) {
        return this.baseMapper.tree(paramCondition);
    }


    public <T> List<LayuiTree> commonTree(CommonParam param) {
        final List<LayuiTree> tree = tree(param);
        return TreeKit.buildTree(param.getStartId(), tree);
    }

    public List<String> getChildDepts(Long parentDeptId) {
        return this.deptService.getBaseMapper().likePids(parentDeptId).parallelStream().map(Dept::getDeptId).map(String::valueOf).collect(Collectors.toList());
    }
}
